
/*
 * Copyright (C) 2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: ImplicitField.java 364 2010-02-25 21:46:44Z archie.cobbs $
 */

package org.dellroad.sidekar;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Support superclass for implicit fields.
 *
 * <p>
 * Implicit fields are special: they are defined automatically for every {@link Entity} and provide a <em>read-only</em>
 * view into the corresponding properties exposed by the {@link Item} interface.
 * Because they are read-only, the {@link #set set()} method always throws {@link UnsupportedOperationException}.
 * </p>
 *
 * <p>
 * Unlike normal fields, implicit fields may be accessed even if the associated {@link Item} is detached.
 * </p>
 *
 * @see Item
 * @see Database#getIdField
 * @see Database#getAttachedField
 * @see Database#getEntityField
 */
public abstract class ImplicitField<T> extends AtomicField<T> {

    ImplicitField(String name, Class<T> type) {
        super(name, null, type);
    }

    @Override
    public abstract T get(Item<?> item);

    /**
     * Implicit fields are read-only, so this method always throws an exception.
     *
     * @throws UnsupportedOperationException always
     */
    @Override
    public final void set(Item<?> item, T value) {
        throw new UnsupportedOperationException();
    }

    @Override
    final void checkName() {
        // normally illegal names are allowed
    }

    @Override
    public final void encode(DataOutputStream output, T value) throws IOException {
        throw new UnsupportedOperationException();
    }

    @Override
    public final T decode(DataInputStream output) throws IOException {
        throw new UnsupportedOperationException();
    }
}

